<?php
/**
*
* @package MoltX QuickChat
* @author $Author: moltendorf $
* @version $Id: molten_chat_retrieve.php 169 2009-09-26 05:23:57Z moltendorf $
* @latest $URL: https://molten-chat.googlecode.com/svn/trunk/source/molten_chat_retrieve.php $
* @copyright (c) 2005-2009 Matthew Oltendorf
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
// Specify the path to you phpBB3 installation directory.
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
// The common.php file is required.
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin(false);
$auth->acl($user->data);

$user->setup();

if (defined('molten_chat_enabled') and ($auth->acl_get('u_molten_chat_user_send') or $auth->acl_get('u_molten_chat_user_view')))
{
	$username = htmlspecialchars_decode($user->data['username'], ENT_COMPAT);

	$color = true;
	$length = strlen ($user->data['user_colour']);

	if ($length == 6)
	{
		$color = array(
			'red' => hexdec($user->data['user_colour'][0].$user->data['user_colour'][1]),
			'green' => hexdec($user->data['user_colour'][2].$user->data['user_colour'][3]),
			'blue' => hexdec($user->data['user_colour'][4].$user->data['user_colour'][5])
		);
	}
	elseif ($length == 3)
	{
		$color = array(
			'red' => hexdec($user->data['user_colour'][0].$user->data['user_colour'][0]),
			'green' => hexdec($user->data['user_colour'][1].$user->data['user_colour'][1]),
			'blue' => hexdec($user->data['user_colour'][2].$user->data['user_colour'][2])
		);
	}

	// Get input.
	$instance = request_var('instance', 'new');
	$level = request_var('level', 0);
	$line = request_var('line', 0);
	$action = request_var('action', 0);

	// Only registered users are handled server-side.
	if ($user->data['is_registered'] and $instance != 'new')
	{
		// Typecase $instance to an integer.
		$instance = (integer) $instance;

		// Update user's time.
		molten_chat_server::update($username, false, false, $color);
	}
	elseif ($instance == 'new')
	{
		// Join the user to the room.
		$instance = molten_chat_server::join($username, false, $user->session_id, $color);
	}

	// Delay execution until new information is available.
	if (molten_chat_server::push($level, $line, $action, array('identifier' => $user->session_id, 'instance' => $instance)))
	{
		// Get new information (if there is any).
		molten_chat_server::retrieve($level, $line, $action, $instance);
	}
}
else
{
	sleep(30);
}